<?php
/**
 * CodeModel
 * 
 * @author Admin
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class Wap_Models_UserModel extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'user';
    protected $_primary = 'UserID';
    
    function checkUser($UserID, $Password){
    	$db = Zend_Db_Table_Abstract::getDefaultAdapter();
    	$mySql = $db->select()
    				->from(array($this->_name),array('UserID'));
        $mySql->where('UserID = ?', $UserID);
    	$mySql->where('Password = ?', $Password);
    //    echo $mySql; die;
    	$result = $db->fetchRow($mySql);
    	return $result;
    }

    function getUserIDbyEmail($email){
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
    	$mySql = $db->select()
    				->from(array($this->_name),'UserID')
                    ->where('email=?',trim($email));
        return $db->fetchOne($mySql);
    }
    
    function getAllUserPlayOnlineByProductID($UserID,$ProductID){
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
    	$mySql = $db->select()
    				->from(array($this->_name),array('UserID','email'))
                    ->join('userproduct','userproduct.UserID = user.UserID','')
                    ->join('userinfo','userinfo.UserID =user.UserID','Username')
                    //->joinLeft('friend','friend.UserID = user.UserID','FriendID')
                    //->joinLeft('friendrequest','friendrequest.SenderID = user.UserID')
                    ->where('ProductID =?',$ProductID)
                    ->where("LastLogin  > SUBTIME(NOW(),'0:10:00')") //users inactive for 10 minutes
                    ->where('user.UserID <> ?',$UserID);
        //echo $mySql;die;
        
        //filter dertermine which people online friend with this user?
        //echo $mySql;die;
        $excute = $db->query($mySql);
        $totalRecords= $excute->RowCount();
       	$result = $db->fetchAll($mySql);
        //Zend_Debug::dump($result);die;
        $arr=array();
        Zend_Loader::loadClass('mobile_models_FriendModel');
        $friend_model=new mobile_models_FriendModel();
        $friend_request_model=new mobile_models_FriendRequestModel();
        foreach($result as $user){
            $isFriend=-1;//not friend
            //check if already friend
             $FriendID=$user['UserID'];
            if($friend_model->checkFriend($UserID,$FriendID)){
                $isFriend=1;
            }else{
                //check if this user sent friend request
                $SenderID=$UserID;
                $ReceiverID=$user['UserID'];
                $request=$friend_request_model->getConfirmStatus($SenderID,$ReceiverID);
              // echo $cfmStatus;die;
                if(isset($request['SenderID'])){
                    //Zend_Debug::dump($request);die;
                    $isFriend = $request['ConfirmStatus'];//0: friend requets sent 1: already friend
                    if($request['ReceiverID'] == $SenderID){
                           $isFriend=-2;//another user send friend request to this user
                    }
                }
            }
            $arr[]=array(
               'UserID' => $user['UserID'],
               'email'  => $user['email'],
               'Username' => $user['Username'],
               'isFriend' => $isFriend,
            );
            
        }
        if(count($arr) > 0){
            $arr[0]['totalUsersPlayOnlineByGameID']=$totalRecords;
        }
       	return $arr;
    }
    
    function getRamdomUsers($email){
        	$db = Zend_Db_Table_Abstract::getDefaultAdapter();
            $mySql = $db->select()
    				->from(array($this->_name),array('UserID'))
                    ->join('userinfo','userinfo.UserID =user.UserID',array('Username','Avatar'))
                    ->where('user.Email <> ?',$email)
                    ->order('RAND()')
                    ->limit(3);
       	    $result = $db->fetchAll($mySql); 
            return $result;
    }
    function loaduserdata($userid,$productID){
    	$db = Zend_Db_Table_Abstract::getDefaultAdapter();
    	$sql = $db->select()->from(array('a'=>'achievements'),array('Total'=>'count(*)'))->where('ProductID=?',$productID)->group('ProductID');
    	$sql1 = $db->select()->from(array('ua'=>'user_achievements'),array('user'=>'Count(*)'))->where('UseriD =?',$userid)->where('ProductID=?',$productID);
//     	echo $sql1;
//     	die;
    	$reach = $db->fetchRow($sql1);
    	$total = $db->fetchRow($sql);
    	return "Đạt được ".$reach['user']." trên ".$total['Total']." danh hiệu";
    	
    }
    function loaduserinfobyid($id){
        $db=Zend_Db_Table_Abstract::getDefaultAdapter();
        $sql  = $db->select()->from('userinfo',array('Username','Avatar'))->where('UserID=?',$id);
//         echo $sql;
//         die;
        return $db->fetchRow($sql);
                
    }
    
}